//
//  RegisterViewController.m
//  联付商圈
//
//  Created by 韩佳睿 on 2017/8/14.
//  Copyright © 2017年 lianfu. All rights reserved.
//

#import "RegisterViewController.h"
#import "YYModel.h"

@interface RegisterViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.observeKeyboard = YES;
    
    self.phoneTextField.delegate = self;
    self.codeTextField.delegate = self;
    self.passwordTextField.delegate = self;
}

- (IBAction)backBtnAction:(id)sender {
    [self popViewController];
}

- (IBAction)getCodeBtnAction:(id)sender {
    NSDictionary *param = @{@"mobile" : _phoneTextField.text};
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
    
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    [manager GET:@"http://www.beidikeji.com/life/public/jiekou/sendsms?" parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        if (dic[@"success"]) {
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
            HUD.labelText = dic[@"success"];
            HUD.mode = MBProgressHUDModeText;
            [HUD hide:YES afterDelay:1.0];
        } else {
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
            HUD.labelText = dic[@"error"];
            HUD.mode = MBProgressHUDModeText;
            [HUD hide:YES afterDelay:1.0];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

- (IBAction)registerBtnAction:(id)sender {
    if (_passwordTextField.text.length < 6) {
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
        HUD.labelText = @"请输入不少于六位密码";
        HUD.mode = MBProgressHUDModeText;
        [HUD hide:YES afterDelay:1.0];
    } else {
        NSDictionary *param = @{@"username" : _phoneTextField.text, @"key" : _codeTextField.text, @"password" : _passwordTextField.text, @"token" : @"220QWER181ASD1994ZXC"};
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil];
        
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.securityPolicy.allowInvalidCertificates = YES;
        
        [manager GET:[Host stringByAppendingString:@"shopzhuce?"] parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
            if (dic[@"success"]) {
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = dic[@"success"][@"message"];
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                [self dismissViewControllerAnimated:YES completion:nil];
            } else {
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = dic[@"error"][@"message"];
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        }];
    }
}

- (void)keyboardWillShow:(NSNotification *)notification {
    [super keyboardWillShow:notification];
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    CGRect frame = self.view.frame;
    frame.origin.y = -keyboardFrame.size.height+(self.view.sizeH-_registerBtn.originY-_registerBtn.sizeH);
    self.view.frame = frame;
}

- (void)keyboardWillHide:(NSNotification *)notification {
    [super keyboardWillHide:notification];
    
    CGRect frame = self.view.frame;
    frame.origin.y = 0;
    self.view.frame = frame;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return true;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
